package com.example.demo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.SEmp;
import com.example.demo.common.exception.DataValidException;
import com.example.demo.mapper.SCustomerMapper;
import com.example.demo.mapper.SDeptMapper;
import com.example.demo.mapper.SEmpMapper;
import com.example.demo.mapper.SOrdMapper;
import com.example.demo.mapper.SRegionMapper;
import com.example.demo.mapper.STitleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SEmpService {
	@Autowired
	private SEmpMapper seMapper;
	@Autowired
	private SDeptMapper sdMapper;
	@Autowired
	private SRegionMapper srMapper;
	@Autowired
	private STitleMapper stMapper;
	@Autowired
	private SOrdMapper soMapper;
	@Autowired
	private SCustomerMapper scMapper;
	//查找所有员工的信息
	public PageInfo<SEmp> findAllByPage(int pageNum, int pageSize){
		PageHelper.startPage(pageNum,pageSize);
		//执行查找行为
		List<SEmp> list =seMapper.selectAll();
		if (list==null) {
			list=new ArrayList<>(0);
		}
		return new PageInfo<>(list);
	}
	
	
	
	
	//查看数量
	public List<Map<String, Object>> countEmp(){
		List<Map<String, Object>> list=seMapper.countEmp();
		return list;
	}
	
	//查找登入用户和密码
	public SEmp findUser(String username) {
		SEmp sEmp=seMapper.selectByUsername(username);
		return sEmp;
	}
	
	
	
	//通过条件查找员工信息
	public PageInfo<SEmp> findByCondition(Integer id,String username,Integer dept_id,Date start,Date end,int pageNum,int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		username=(username==null||"".equals(username))?"":"%"+username+"%";
		List<SEmp> list=seMapper.selectByContidion(id, username, dept_id, start, end);
		if (list==null) {
			list=new ArrayList<>(0);
		}
		return new PageInfo<>(list);
	}
	
	//添加或保存员工信息
	public void saveOrUpdate(SEmp se) {
		//判断输入的信息是否存在
		if (se.getDeptId()==null||se.getManagerId()==null||se.getTitle()==null) {
			//判断外键是否完整
			throw new DataValidException("请填写完整信息");
		}
		else {
			if (seMapper.selectByPrimaryKey(se.getManagerId())==null||
				sdMapper.selectByPrimaryKey(se.getDeptId())==null||
				stMapper.selectByName(se.getTitle())==null
				
					) {
				
				throw new DataValidException("外键信息不存在");
				
			}
		}
		if (se.getId()==null) {
			seMapper.insert(se);
		}else {
			seMapper.updateByPrimaryKey(se);
		}
	}
	
	//删除员工信息
	//作为customer，ord，emp的外键存在
	//删除前要判断下属员工，订单，顾客信息是否存在
	public int deleteById(Integer id) {
		//判断id是否为空
		if (id==null) {
			throw new DataValidException("请输入员工id");
		}
		//按照员工id查找信息，如果找不到提示删除错误
		if (seMapper.selectByPrimaryKey(id)==null) {
			throw new DataValidException("删除失败");
		}
		SEmp se=seMapper.selectByPrimaryKey(id);
		//判断是否存在下属信息
		if ((!scMapper.selectBySalesRepId(id).isEmpty()||
			!soMapper.selectBySaleRepId(id).isEmpty())||
			!seMapper.selectByManageId(id).isEmpty()
			) {
			throw new DataValidException("存在对应的下属信息，客户，订单，和员工,请删除对应下属信息后在对本条数据进行删除");
		}
		seMapper.deleteByPrimaryKey(id);
		return 0;
	}
	
	//批量删除员工信息
	public int batchDeleteById(int [] id) {
		//判断是否为空
		if (id.length==0||id==null) {
			throw new DataValidException("请选择一批id");
		}
		//查找要删除的员工信息
		for(int i:id) {
			if (seMapper.selectByPrimaryKey(i)==null) {
				throw new DataValidException("删除失败");
			}
			SEmp se=seMapper.selectByPrimaryKey(i);
			//判断是否存在下属信息
			if (!scMapper.selectBySalesRepId(se.getId()).isEmpty()||
				!soMapper.selectBySaleRepId(se.getId()).isEmpty()||
				!seMapper.selectByManageId(se.getId()).isEmpty()) {
				throw new DataValidException("存在对应的下属信息，客户，订单，和员工,请删除对应下属信息后在对本条数据进行删除");
			}
		}
		//删除员工信息
		seMapper.batchDelete(id);
		return 0;
	}
}
